#include "Server.hpp"
#include <memory>
enum
{
    DIV_ERR = 1,
    MOD_ERR,
    OP_ERR
};
static void Usage(string proc)
{
    cout << "\nUsage:\n\t" << proc << " local_port\n\n";
}
void calFunc(Request &req, Response &resp)
{
    switch (req._op)
    {
    case '+':
    {
        resp._exitcode = 0;
        resp._result = req._x + req._y;
    }
    break;
    case '-':
    {
        resp._exitcode = 0;
        resp._result = req._x - req._y;
    }
    break;
    case '*':
    {
        resp._exitcode = 0;
        resp._result = req._x * req._y;
    }
    break;
    case '/':
    {
        if (req._y == 0)
        {
            resp._exitcode = DIV_ERR;
            resp._result = 0;
        }
        else
        {
            resp._exitcode = 0;
            resp._result = req._x / req._y;
        }
    }
    break;
    case '%':
    {
        if (req._y == 0)
        {
            resp._exitcode = MOD_ERR;
            resp._result = 0;
        }
        else
        {
            resp._exitcode = 0;
            resp._result = req._x % req._y;
        }
    }
    break;
    default:
        resp._result = OP_ERR;
        break;
    }
}
int main(int argc, char *argv[])
{
    if (argc != 2)
    {
        Usage(argv[0]);
        exit(3);
    }
    uint16_t port = atoi(argv[1]);
    unique_ptr<Server> usvr(new Server(port));
    usvr->init();
    usvr->start(calFunc);
    return 0;
}